package com.weight.controller;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weight.controller.utils.Result;
import com.weight.entity.TokenEntity;
import com.weight.entity.User;
import com.weight.service.UserService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.UUID;

@RestController
@RequestMapping
public class UserController {
    @Autowired
    private UserService userservice;

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        if (!StringUtils.hasLength(user.getUsername()) || !StringUtils.hasLength(user.getPwd())) return Result.error("用户名或者密码不能为空");
        //根据用户名查用户
        QueryWrapper<User> qw = new QueryWrapper<>();
        int count = userservice.count(qw.eq("username",user.getUsername()));
        if (count <= 0) return Result.error("用户不存在，请先注册");
        //校验密码
        if (!user.getPwd().equals(userservice.getOne(qw.select("pwd").eq("username",user.getUsername())).getPwd())) return Result.error("密码错误");
        User user1 = userservice.getOne(new QueryWrapper<User>().eq("username",user.getUsername()));
        String id = user1.getId();
        //sa-token 登录
        StpUtil.login(id);
        // 获取token
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        SaSession session = StpUtil.getSession();
        // 设置用户信息
        session.set("user",user1);
        return Result.success(new TokenEntity(tokenInfo.getTokenValue()),"登录成功");
    }

    //获取用户信息
    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        SaSession session = StpUtil.getSession();
        System.out.println(session);
        return Result.success(session.getDataMap().get("user"),"ok");
    };

    //用户注册
    @PostMapping("/regist")
    public Result register(@RequestBody User user){
        return userservice.regist(user);

//        if(!StringUtils.hasLength(user.getUsername())){
//            return Result.error("用户名不能为空");
//        }if(!StringUtils.hasLength(user.getPwd())){
//            return Result.error("密码不能为空");
//        }if (!StringUtils.hasLength(user.getPwd2())){
//            return Result.error("确认密码不能为空");
//        }if (!user.getPwd().equals(user.getPwd2())){
//            return Result.error("确认密码和原密码不一样");
//        }if (!StringUtils.hasLength(user.getEmail())){
//            return Result.error("邮箱不能为空");
//        }if (!user.getEmail().matches("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+")) {
//            return Result.error("请输入正确的邮箱");
//        }if (!StringUtils.hasLength(user.getCode())){
//            return Result.error("验证码不能为空");
//        }
//        int count = userservice.count(new QueryWrapper<User>().eq("email",user.getEmail()));
//        if (count>0) return Result.error("已经注册过了");
//        //生成id
//        user.setId(String.valueOf(UUID.randomUUID()).replaceAll("-",""));
//        userservice.save(user);
//
//        return Result.success(null,"注册成功");
    }

    //发送验证码
    @GetMapping("getCodeByEmail")
    public Result getCodeByEmail(String email){
        return userservice.sendCode(email);
    }

    //用户列表
    @GetMapping("/userList")
    public Result getUserList(Integer currentPage,Integer pageSize){
        IPage page = new Page(currentPage,pageSize);
        val data = userservice.page(page, null);
        return Result.success(data,"获取列表成功");
    }
}
